package com.frank.loadinglibrary.common.element;

import com.frank.loadinglibrary.base.baseanimations.BaseAnimationController;
import com.frank.loadinglibrary.base.baseelement.BaseElement;
import com.frank.loadinglibrary.base.baseelement.IComponentCallback;
import com.frank.loadinglibrary.utils.SimpleAnimatorListener;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;

import java.util.ArrayList;

public class MagnifyElement extends BaseElement implements BaseAnimationController.IAnimationController<Animator> {

    Paint mPaint;
    String pillarColor = "#0000ff";
    String glassColor = "#00ff00";
    private int durtion;
    private int padding;

    public MagnifyElement(IComponentCallback callback) {
        super();
        this.callback = callback;
    }

    @Override
    public void initPaint() {
        super.initPaint();
        if (mPaint == null) {
            mPaint = new Paint();
        }
        mPaint.setStyle(Paint.Style.FILL_STYLE);
        mPaint.setColor(new Color());
        mPaint.setAntiAlias(true);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        mPaint.setColor(new Color(Color.getIntColor(pillarColor)));
        float centerX = 1.9f * getWidth() / 3f;
        float centerY = 1.9f * getHeight() / 3f;
        float left = centerX - getWidth() / 2f;
        float top = centerX - 0.05f * getWidth();
        float right = centerX;
        float bottom = centerY + 0.05f * getWidth();
        canvas.rotate(45, centerX, centerY);
        canvas.drawRect(left, top, right, bottom, mPaint);
        canvas.restore();
        canvas.rotate(-45, centerX, centerY);
        canvas.drawCircle(getWidth() / 4, getHeight() / 4, getWidth() / 4, mPaint);
        mPaint.setColor(new Color(Color.getIntColor(glassColor)));
        canvas.drawCircle(getWidth() / 4, getHeight() / 4, getWidth() / 6, mPaint);
    }

    @Override
    public ArrayList<Animator> onCreateAnimators() {
        ArrayList<Animator> animators = new ArrayList<>();
        int distance = (int) (getWidth() - getRightEdge() - 2 * padding);

        AnimatorProperty animatorPropertyOne = callback.getAnimatorProperty();
        animatorPropertyOne.moveByX(distance)
                .setDuration((long) (durtion / 3.8))
                .setLoopedCount(0);


        AnimatorProperty animatorPropertyTwo = callback.getAnimatorProperty();
        animatorPropertyTwo.moveByX(-distance / 2f)
                .moveByY(distance)
                .setDuration((long) (durtion * 1.4 / 3.8));


        AnimatorProperty animatorPropertyThree = callback.getAnimatorProperty();
        animatorPropertyThree.moveByX(-distance / 2f)
                .moveByY(-distance)
                .setDuration((long) (durtion * 1.4 / 3.8));

        animatorPropertyOne.setStateChangedListener(new SimpleAnimatorListener() {
            @Override
            public void onEnd(Animator animator) {
                super.onEnd(animator);
                animatorPropertyTwo.start();
            }
        }).start();

        animatorPropertyTwo.setStateChangedListener(new SimpleAnimatorListener() {
            @Override
            public void onEnd(Animator animator) {
                super.onEnd(animator);
                animatorPropertyThree.start();
            }
        });

        animatorPropertyThree.setStateChangedListener(new SimpleAnimatorListener() {
            @Override
            public void onEnd(Animator animator) {
                super.onEnd(animator);
                animatorPropertyOne.start();
            }
        });
        animators.add(animatorPropertyOne);
        return animators;
    }

    @Override
    public void onAnimationReset() {
        //do nothing because donot need to reset animation params
    }

    public float getRightEdge() {
        return (float) (1.9f * getWidth() / 3f + 0.036 * getWidth());
    }

    private float getBottomEdge() {
        return (float) (1.9f * getHeight() / 3f + 0.036 * getWidth());
    }

    @Override
    public BaseAnimationController.IAnimationController<Animator> initAnimationController() {
        return this;
    }

    public void apply(MagnifyBuilder builder) {
        this.callback = builder.callback;
        this.durtion = builder.durtion;
        this.pillarColor = builder.pillarColor;
        this.glassColor = builder.glassColor;
        this.padding = builder.padding;
    }

    public static class MagnifyBuilder {
        IComponentCallback callback;
        private int durtion = 3000;
        private int padding = 20;
        String pillarColor = "#0000ff";
        String glassColor = "#00ff00";

        public MagnifyBuilder(IComponentCallback iComponentCallback) {
            this.callback = iComponentCallback;
        }

        public MagnifyBuilder setDurtion(int durtion) {
            this.durtion = durtion;
            return this;
        }

        public MagnifyBuilder setPadding(int padding) {
            this.padding = padding;
            return this;
        }

        public MagnifyBuilder setPillarColor(String pillarColor) {
            this.pillarColor = pillarColor;
            return this;
        }

        public MagnifyBuilder setGlassColor(String glassColor) {
            this.glassColor = glassColor;
            return this;
        }

        public MagnifyElement build() {
            MagnifyElement element = new MagnifyElement(callback);
            element.apply(this);
            return element;
        }
    }
}
